package com.baturu.vin.ford.dal.dao;

import com.baturu.vin.ford.dal.entity.FordPartsInfoEntity;
import com.baturu.vin.ford.dto.FordImageDTO;
import com.baturu.vin.ford.dto.FordReplaceDTO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Collection;
import java.util.List;
import java.util.Set;

public interface FordPartsInfoDAO {

    /**
     * 查询配件信息
     *
     * @param catalogCode 车型目录
     * @param partsCodes  配件编码
     * @return 配件信息
     */
    @Select({"<script>",
            "SELECT ",
            "i.id, i.partsCode, i.catalogCode, i.sectionCode, i.beginDate, i.endDate, i.label, i.callout, i.qty, i.chLabel",
            "FROM ford_parts_info i",
            "WHERE 1=1 ",
            "<if test='catalogCode != null'>",
            "AND catalogCode in ( #{catalogCode},concat('C',#{catalogCode}))",
            "</if>",
            "AND i.partsCode in ",
            "<foreach item='item' index='index' collection='partsCodes' open='(' separator=', ' close=') '>",
            "#{item}",
            "</foreach>",
            "</script>"})
    List<FordPartsInfoEntity> queryByPartsCode(@Param("catalogCode") String catalogCode, @Param("partsCodes") Set<String> partsCodes);

    @Select({"<script>",
            "select distinct h.catalogCode, h.sectionCode, h.callout, ",
            "h.topLeftX, h.topLeftY, h.bottomRightX, h.bottomRightY,s.largeImagePaths as imagePath, s.label, s.chLabel",
            "from ford_parts_hotpoints as h",
            "join ford_section as s",
            "on h.catalogCode = s.catalogCode and h.sectionCode = s.`data`",
            "where (h.catalogCode, h.sectionCode, h.callout) in ",
            "<foreach item='item' collection='partsInfo' index='index' open='(' separator=',' close=')'>",
            "(#{item.catalogCode}, #{item.sectionCode}, #{item.callout})",
            "</foreach>",
            "</script>"})
    List<FordImageDTO> queryByPartsInfo(@Param("partsInfo") Collection<FordPartsInfoEntity> partsInfo);

    @Select({"<script>",
            "select distinct partsCode from ford_parts_info",
            "where catalogCode = #{catalogCode}",
            "and sectionCode in",
            "<foreach item='item' index='index' collection='imageCodes' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"})
    Set<String> queryPartsCodeByCatalogAndSection(@Param("catalogCode") String catalogCode,
                                                  @Param("imageCodes") Collection<String> imageCodes);

    @Select({"<script>",
            "select distinct catalogCode, sectionCode, partsCode, newestCode from ford_replace_info",
            "where catalogCode = #{catalogCode}",
            "and partsCode in",
            "<foreach item='item' index='index' collection='partsCodes' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"})
    List<FordReplaceDTO> queryNewestReplacePartsCode(@Param("catalogCode") String catalogCode,
                                                     @Param("partsCodes") Collection<String> partsCode);

    @Select({"<script>",
            "select distinct catalogCode, sectionCode, partsCode, replaceCode from ford_replace_info",
            "where catalogCode = #{catalogCode}",
            "and partsCode in",
            "<foreach item='item' index='index' collection='partsCodes' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"})
    List<FordReplaceDTO> queryReplacePartsCode(@Param("catalogCode") String catalogCode,
                                               @Param("partsCodes") Collection<String> partsCode);

}